var userServerAdr="http://user.credit-manage.com";//本地
var serverZjrAdr="http://loan.credit-manage.com";//本地
var accountServerAdr="http://account.credit-manage.com";//本地
var blackServerAdr="http://www.credit-manage.com/";//服务器

angular.module('starter.commonService', [])

//图片上传Service
.factory('CommonService', function($http, $q) {

  return {
  		//上传图片
	    uploadPic: function(imageURI) {
		      var deferred = $q.defer();
					var options = new FileUploadOptions();
			    options.fileKey = "file",
			    options.fileName = imageURI.substr(imageURI.lastIndexOf('/')+1);
			    options.mimeType = "image/jpeg";
			    var ft = new FileTransfer();
			    // 上传回调
			    //ft.onprogress = showUploadingProgress;
			    //navigator.notification.progressStart("", "当前上传进度");
			    ft.upload( imageURI, encodeURI('http://image.credit-manage.com:8058/common/uploadPic.htm'), function(data){
			    		var resp = JSON.parse(data.response);
			        deferred.resolve(resp);
			        //navigator.notification.progressStop();
			    } , null, options);
			   	return deferred.promise;
	    },
	    //上传图片
	    uploadHeadPic: function(imageURI) {
		      var deferred = $q.defer();
					var options = new FileUploadOptions();
			    options.fileKey = "file",
			    options.fileName = imageURI.substr(imageURI.lastIndexOf('/')+1);
			    options.mimeType = "image/jpeg";
			    var ft = new FileTransfer();
			    // 上传回调
			    //ft.onprogress = showUploadingProgress;
			    //navigator.notification.progressStart("", "当前上传进度");
			    ft.upload( imageURI, encodeURI('http://image.credit-manage.com:8058/common/uploadHeadPic.htm'), function(data){
			    		var resp = JSON.parse(data.response);
			        deferred.resolve(resp);
			        //navigator.notification.progressStop();
			    } , null, options);
			   	return deferred.promise;
	    },
	    //获取所有通讯录联系人
	    getAllContacts: function() {
		      var deferred = $q.defer();
					var options = new ContactFindOptions();  
							options.filter = ""; 
				    	options.multiple = true; 
				  var fields = ["displayName", "phoneNumbers"];  
					navigator.contacts.find(fields, function(allContacts){
							deferred.resolve(allContacts);
					}, null, options);
			   	return deferred.promise;
	    },
	    //登录
	    login:function(phone,userPassword){
	  		var deferred = $q.defer();
				$http({
					method:'post',
					url:userServerAdr+"/app/login.htm",
					params:{"phone":phone,"userPassword":userPassword}
					}).success(function (response) {
					  	deferred.resolve(response);
					}).error(function(data) {
				       deferred.reject();
					});
		   		return deferred.promise;
	  	},
	  	//注册
	    register:function(phone,userPassword,type,phoneCode,machineCode){
	  		var deferred = $q.defer();
				$http({
					method:'post',
					url:userServerAdr+"/app/register.htm",
					params:{"phone":phone,"userPassword":userPassword,"type":type,"phoneCode":phoneCode,"machineCode":machineCode}
					}).success(function (response) {
					  	deferred.resolve(response);
					}).error(function(data) {
				       deferred.reject();
					});
		   		return deferred.promise;
	  	},
	  	//发送验证码
	  	sendSmsCode:function(phone){
	  		var deferred = $q.defer();
				$http({
					method:'post',
					url:userServerAdr+"/app/sendSmsCode.htm",
					params:{"phone":phone}
					}).success(function (response) {
					  	deferred.resolve(response);
					}).error(function(data) {
				       deferred.reject();
					});
		   		return deferred.promise;
	  	},
	  	//发送验证码
	  	sendSmsCodeByUserId:function(){
	  		var deferred = $q.defer();
				$http({
					method:'post',
					url:userServerAdr+"/app/sendSmsCodeByUserId.htm"
					}).success(function (response) {
					  	deferred.resolve(response);
					}).error(function(data) {
				       deferred.reject();
					});
		   		return deferred.promise;
	  	},
	  	//获取版本号
	  	getAppInfo:function(type){
	  		var deferred = $q.defer();
				$http({
					method:'post',
					url:userServerAdr+"/app/getAppInfo.htm",
					params:{"type":type}
					}).success(function (response) {
					  	deferred.resolve(response);
					}).error(function(data) {
				       deferred.reject();
					});
		   		return deferred.promise;
	  	},
	  	//重置密码
	  	resetLoginPassword:function(data){
	  		var deferred = $q.defer();
				$http({
					method:'post',
					url:userServerAdr+"/app/resetLoginPassword.htm",
					params:{"phone":data.phone,"smsCode":data.smsCode,"password":data.password}
					}).success(function (response) {
					  	deferred.resolve(response);
					}).error(function(data) {
				       deferred.reject();
					});
		   		return deferred.promise;
	  	},
	  	//点击贷款负债数字进入-长贷
	  	getLongProduceByUserId:function(customerId,page){
	  		var deferred = $q.defer();
				$http({
					method:'post',
					url:serverZjrAdr+"/app/user/getLongProduceByUserId.htm",
					params:{"customerId":customerId,"page":page}
					}).success(function (response) {
					  	deferred.resolve(response);
					}).error(function(data) {
				       deferred.reject();
					});
		   		return deferred.promise;
	  	},
	  	//点击贷款负债数字进入-长贷-明细
	  	getLongProduceDetailById:function(longProduceId){
	  		var deferred = $q.defer();
				$http({
					method:'post',
					url:serverZjrAdr+"/app/user/getLongProduceDetailById.htm",
					params:{"longProduceId":longProduceId}
					}).success(function (response) {
					  	deferred.resolve(response);
					}).error(function(data) {
				       deferred.reject();
					});
		   		return deferred.promise;
	  	},
	  	//点击贷款负债数字进入-短贷
	  	getProduceByUserId:function(customerId,page){
	  		var deferred = $q.defer();
				$http({
					method:'post',
					url:serverZjrAdr+"/app/user/getProduceByUserId.htm",
					params:{"customerId":customerId,"page":page}
					}).success(function (response) {
					  	deferred.resolve(response);
					}).error(function(data) {
				       deferred.reject();
					});
		   		return deferred.promise;
	  	},
	  	//保存联系人
	  	saveContacts:function(map){
	  		var deferred = $q.defer();
				$http({
					method:'post',
					url:"http://www.credit-manage.com:8066/cloudsys/saveContacts.htm",
					params:{"userId":map.userId,"contacts":encodeURI(map.contacts)}
				}).success(function (response) {
					  	deferred.resolve(response);
					}).error(function(data) {
				       deferred.reject();
					});
		   		return deferred.promise;
	  	},
    	//查询高级认证信息数据
	  	getJuHeRecordByUserId:function(func,customerId){
	  		var deferred = $q.defer();
				$http({
					method:'post',
					url:userServerAdr+"/app/getJuHeRecordByUserId.htm",
					params:{"func":func,"customerId":customerId}
				}).success(function (response) {
					  deferred.resolve(response);
				}).error(function(data) {
			      deferred.reject();
				});
	   		return deferred.promise;
	  	},
	  	//查询高级认证信息数据
	  	getJuHeRecordByUserId:function(func,customerId){
	  		var deferred = $q.defer();
				$http({
					method:'post',
					url:userServerAdr+"/app/getJuHeRecordByUserId.htm",
					params:{"func":func,"customerId":customerId}
				}).success(function (response) {
					  deferred.resolve(response);
				}).error(function(data) {
			      deferred.reject();
				});
	   		return deferred.promise;
	  	}
    
  };
})



;
